package com.matrix.draft.sockettest.http;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class HttpServer {
    public static Integer nRequests = 0;
    private String HOST = "127.0.0.1";
    private Integer PORT = 8080;

    public void start() {
        // 准备2个事件组和一个启动器
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        try {
            // 设置并启动服务器
            Channel ch = serverBootstrap
                    .group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new HttpSocketInitializer())
                    .bind(HOST, PORT) // 启动
                    .sync()
                    .channel();
            System.err.println("NettyHttpServer is running at "+HOST+":"+PORT+"...");
            ch.closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) {
        new HttpServer().start();
    }
}
